import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.Random;
import edu.ucdavis.jr.*;
import edu.ucdavis.jr.jrx.*;

public class Main {
	private static int bucketSize;
	private static int  processes;
	private static int N; // for convenience.
	private static int[] array;
	private static long start;
	private static long end;
	private static boolean displayArray = false;
	private static long total;

	public static void main(String[] args) {
		// Parse the first argument - bucket size.
		bucketSize = Integer.parseInt(args[1]);
		N = bucketSize; // for convenience.
		File file = new File(args[1]);
		
		// Parse the second argument - number of processes.
		processes = Integer.parseInt(args[2]);
		BucketSort bucketSort = new BucketSort(processes);
		
		// Parse the third argument if specified whether to display the array elements.
		if( args.length == 4 && args[3] != null ) {
			displayArray = args[3].equalsIgnoreCase("v");
		}
					
		array = generateUnsortedArray(bucketSize);

		if( displayArray ) {
			System.out.print( "Before: ");
			for( int i=0; i<array.length; i++){
				System.out.print( array[i] + ", ");
			}
		}
		
		start = System.currentTimeMillis();
		
		// Initialize buckets.
		int[] buckets = new int[bucketSize];
		for (int i = 0; i < bucketSize; i++) {
			buckets[i] = 0;
		}

		// Scatter.
		for (int i = 0; i < N; i++) {
			++buckets[array[i]];
		}

		
		bucketSort.compute(array, buckets, bucketSize);
		
		end = System.currentTimeMillis();
		total = end - start;
		
		try {
			JR.registerQuiescenceAction(printResult);
		} catch( QuiescenceRegistrationException ex) {
			ex.printStackTrace();
		}
	}
	
	private static int[] getArrayFrom(File file) {
		BufferedReader reader;
		String fileName = file.getName();
		System.out.println(fileName);

		int endIndex = fileName.indexOf(".");
		String total = fileName.substring(7, endIndex);

		int size = Integer.parseInt(total);
		System.out.println(size);
		int[] array = new int[size];
		int index = 0;
		try {

			reader = new BufferedReader(new FileReader(file));
			String line = null;

			while ((line = reader.readLine()) != null) {
				String[] split = line.split(",");
				for (int i = 0; i < split.length; i++) {
					int value = Integer.parseInt(split[i]);
					System.out.println(value);
					array[index] = value;
					index++;

				}
			}
		} catch (IOException x) {
			System.err.format("IOException: %s%n", x);
		}

		return array;
	}


	private static op void printResult() {
		
		if( displayArray ) {
			System.out.println();
			System.out.print( "After: ");
			for( int i=0; i<array.length; i++){
				System.out.print( array[i] + ", ");
			}
		}
		System.out.println();
		System.out.println( "Bucket size: " + bucketSize);
		System.out.println( "Processes: " + processes);
		System.out.println("Total running time (ms): " + total);
	}
	
	private static int[] generateUnsortedArray(int size) {
		int[] a = new int[size];
		for (int i = 0; i < size; i++) {
			Random random = new Random();
			a[i] = random.nextInt(size);
		}
		return a;
	}

}
